package cm.utils;

import cm.annotation.Mobile;
import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author: yxh
 * @Description: 手机号校验
 * @Date: Created in 2022/9/2 16:18
 * @Modified By:
 */
public class MobileUtil implements ConstraintValidator<Mobile, String> {

    private static final Pattern PATTERN_MOBILE = Pattern.compile("手机正则");

    @Override
    public void initialize(Mobile annotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 如果手机号为空，默认不校验，即校验通过
        if (StrUtil.isEmpty(value)) {
            return true;
        }
        // 校验手机
        return isMobile(value);
    }
    public static boolean isMobile(String mobile) {
        if (StrUtil.length(mobile) != 11) {
            return false;
        }
        //手机号校验
//        return PATTERN_MOBILE.matcher(mobile).matches();
        return true;
    }
}
